package com.woniu.zsd.user.config;



import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {


    @Resource
    private ConnectionFactory connectionFactory;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    //交换机
    private static final String SEND_SIGNUP_CODE_EXCHANGE = "send.signup.code.ex";
    private static final String DELAY_EXCHANGE = "delay.ex";
    private static final String WAITING_EXCHANGE = "waiting.ex";
    //队列
    private static final String SEND_SIGNUP_CODE_QUEUE = "send.signup.code.queue";
    private static final String WAITING_DELETE_TOKEN_QUEUE = "waiting.delete.token.queue";
    //路由键
    private static final String SIGN_UP_CODE_ROUTING_KEY = "send.signup.code";
    private static final String WAITING_DELETE_TOKEN_ROUTING_KEY = "waiting.delete.token";
    private static final String DELAY_DELETE_TOKEN_ROUTING_KEY = "delay.delete.token";
    @Bean
    public DirectExchange sendSignUpCodeExchange(){
        return new DirectExchange(SEND_SIGNUP_CODE_EXCHANGE);
    }
    @Bean
    public Queue sendSignUpCodeQueue(){
        return new Queue(SEND_SIGNUP_CODE_QUEUE, true);
    }
    @Bean
    public Binding bindSendSignUpCodeQueueToSendSignUpCodeExchange(){
        return BindingBuilder.bind(sendSignUpCodeQueue())
                .to(sendSignUpCodeExchange())
                .with(SIGN_UP_CODE_ROUTING_KEY);
    }
    @Bean
    public RabbitMQCallback rabbitMQPublisherAck(RabbitTemplate rabbitTemplate, StringRedisTemplate stringRedisTemplate){
        RabbitMQCallback rabbitMQCallback = new RabbitMQCallback();
        rabbitMQCallback.setStringRedisTemplate(stringRedisTemplate);
        rabbitMQCallback.setRabbitTemplate(rabbitTemplate);
        rabbitTemplate.setConfirmCallback(rabbitMQCallback);
        rabbitTemplate.setReturnCallback(rabbitMQCallback);
        return rabbitMQCallback;
    }
    //创建一个等待交换机
    @Bean
    public DirectExchange waitingExchange(){
        return new DirectExchange(WAITING_EXCHANGE);
    }


    @Bean
    public Queue waitingDeleteTokenQueue(){
        Map<String, Object> params = new HashMap<>();
        params.put("x-message-ttl", 60 * 1000);
        params.put("x-dead-letter-exchange", DELAY_EXCHANGE);
        params.put("x-dead-letter-routing-key", DELAY_DELETE_TOKEN_ROUTING_KEY);
        return new Queue(WAITING_DELETE_TOKEN_QUEUE, true, false, false, params);
    }
    @Bean
    public Binding bindWaitingDeleteTokenQueueToWaitingExchange(){
        return BindingBuilder.bind(waitingDeleteTokenQueue())
                .to(waitingExchange())
                .with(WAITING_DELETE_TOKEN_ROUTING_KEY);
    }
}
